﻿using System;
using System.Collections;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace NetTopologySuite.Geometries
{
	[Serializable]
	public class MultiPolygon : GeometryCollection, IMultiPolygon, IMultiSurface, IGeometryCollection, IGeometry, ICloneable, IComparable, IComparable<IGeometry>, IEquatable<IGeometry>, IEnumerable<IGeometry>, IEnumerable, IPolygonal
	{
		public MultiPolygon(IPolygon[] polygons) : this(polygons, Geometry.DefaultFactory)
		{
		}

		public MultiPolygon(IPolygon[] polygons, IGeometryFactory factory) : base(polygons, factory)
		{
		}

		public override Dimension Dimension
		{
			get
			{
				return Dimension.Surface;
			}
		}

		public override Dimension BoundaryDimension
		{
			get
			{
				return Dimension.Curve;
			}
		}

		public override string GeometryType
		{
			get
			{
				return "MultiPolygon";
			}
		}

		public override OgcGeometryType OgcGeometryType
		{
			get
			{
				return OgcGeometryType.MultiPolygon;
			}
		}

		public override IGeometry Boundary
		{
			get
			{
				if (this.IsEmpty)
				{
					return base.Factory.CreateMultiLineString(null);
				}
				List<ILineString> list = new List<ILineString>();
				for (int i = 0; i < base.Geometries.Length; i++)
				{
					IGeometry boundary = ((IPolygon)base.Geometries[i]).Boundary;
					for (int j = 0; j < boundary.NumGeometries; j++)
					{
						list.Add((ILineString)boundary.GetGeometryN(j));
					}
				}
				return base.Factory.CreateMultiLineString(list.ToArray());
			}
		}

		public override bool EqualsExact(IGeometry other, double tolerance)
		{
			return this.IsEquivalentClass(other) && base.EqualsExact(other, tolerance);
		}

		public override IGeometry Reverse()
		{
			IPolygon[] array = new IPolygon[base.Geometries.Length];
			for (int i = 0; i < base.Geometries.Length; i++)
			{
				array[i] = (Polygon)base.Geometries[i].Reverse();
			}
			return base.Factory.CreateMultiPolygon(array);
		}

		public new static readonly IMultiPolygon Empty = new GeometryFactory().CreateMultiPolygon(null);
	}
}
